<!-- 新增送修-科室 -->
<template>
	<ListLayout>
		<!-- 送修方式 -->
		<AddRepairMethod @onType="(val) => type=val"></AddRepairMethod>
		<!-- 送修方式-工程师-送修信息 -->
		<template v-if="type == 2">
			<AddSendRepairEngineerCard ref="AddSendRepairEngineerCard"></AddSendRepairEngineerCard>
		</template>
		<template v-else-if="type == 1">
			<!-- 送修方式-科室-送修信息 -->
			<AddSendRepairDepartmentCard ref="AddSendRepairDepartmentCard"></AddSendRepairDepartmentCard>
		</template>
		<template v-else>
			<!-- 送修方式-现场维修？-送修信息 -->
		</template>
		<!-- 工程师、现场维修-设备信息 -->
		<AddDeviceCard ref="AddDeviceCard"></AddDeviceCard>
		<!-- 工程师、现场维修-科室信息 -->
		<AddDeparmentCard ref="AddDeparmentCard"></AddDeparmentCard>
		<template v-if="['1','3'].includes(type)">
			<!-- 科室-现场维修-邀标 -->
			<AddInviteCard ref="AddInviteCard" @onChecked="(val)=> isChecked = val"></AddInviteCard>
		</template>
		<!-- 是否邀标 -->
		<template v-if="isChecked">
			<template v-if="['1'].includes(type)">
				<!-- 科室-厂家信息 -->
				<template v-for="(item,index) in factoryList">
					<AddFactoryCard :ref="'AddFactoryCard'+index" :index="index" :key="index" @onDelete="onDeleteFactory(index)"></AddFactoryCard>
				</template>
			</template>
			<view class="u-m-t-20">
				<HButton plain borderStyle="dashed" @click="addFactory">添加厂家</HButton>
			</view>
		</template>
		<template v-else>
			<template v-if="['1','3'].includes(type)">
				<!-- 科室-厂家信息 -->
				<AddFactoryInfoCard ref="AddFactoryInfoCard"></AddFactoryInfoCard>
			</template>
		</template>

		<!-- 附件 -->
		<AttachmentCard ref="AttachmentCard"></AttachmentCard>
		<template #footer>
			<HButtonGroup @leftClick="leftClick" @rightClick="rightClick">
				<template #left>
					取消
				</template>
				<template #right>
					提交送修
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>
</template>

<script>
	import ListLayout from '@/components/layout/listLayout.vue'

	import AddRepairMethod from '@/components/card/sendRepair/department/info/addRepairMethod.vue'
	import AddSendRepairDepartmentCard from '@/components/card/sendRepair/department/info/addSendRepairDepartment.vue'
	import AddSendRepairEngineerCard from '@/components/card/sendRepair/department/info/addSendRepairEngineer.vue'


	import HButton from '@/components/button/index.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'


	import AddDeviceCard from '@/components/card/sendRepair/card/common/addDevice.vue'
	import AddDeparmentCard from '@/components/card/sendRepair/card/common/addDeparment.vue'
	import AddInviteCard from '@/components/card/sendRepair/department/info/addInvite.vue'
	import AddFactoryCard from '@/components/card/sendRepair/department/info/addFactory.vue'
	import AddFactoryInfoCard from '@/components/card/sendRepair/department/info/addFactoryInfo.vue'
	import AttachmentCard from '@/components/card/attachment.vue'
	import ReapirApi from '@/apis/repair.js'
	import { factoryState } from '@/sendRepair/factory/state.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	import SendRepairRouterConfig from '@/sendRepair/routerConfig.js'
	export default {
		components: {
			HButton,
			HButtonGroup,
			ListLayout,
			AddRepairMethod,
			AddSendRepairDepartmentCard,
			AddSendRepairEngineerCard,

			AddDeparmentCard,
			AddDeviceCard,
			AttachmentCard,
			AddInviteCard,
			AddFactoryCard,
			AddFactoryInfoCard
		},
		data() {
			return {
				type: '',
				isChecked: false,

				factoryList: ['', '']
			}
		},
		methods: {
			addFactory() {
				this.factoryList.push('')
			},
			onDeleteFactory(index) {
				this.factoryList.splice(index, 1)
			},

			leftClick() {
				wx.navigateBack()
			},
			// 提交送修
			async rightClick() {
				let {
					AddSendRepairEngineerCard,
					AddSendRepairDepartmentCard,
					AddDeviceCard,
					AddDeparmentCard,
					AddInviteCard,
					AddFactoryInfoCard,
					AttachmentCard
				} = this.$refs
				let repairUser = ''
				let repairPhone = ''
				let sendReceiverUser = ''
				let sendReceiverUserPhone = ''
				let sendReceiverDeptId = ''
				if (this.type == 2) {
					if (!AddSendRepairEngineerCard.validate()) {
						return
					}
				}
				if (this.type == 1) {
					if (!AddSendRepairDepartmentCard.validate()) {
						return
					}
				}

				if (AddDeviceCard.validate() && AddDeparmentCard.validate()) {
					if (this.type == 2) {
						let { phone, user } = AddSendRepairEngineerCard.$data
						repairUser = user
						repairPhone = phone

					} else if (this.type == 1) {
						let { phone, sendUser, receiverUser, receiverUserPhone, depId } = AddSendRepairDepartmentCard.$data.formData
						repairUser = sendUser
						repairPhone = phone
						sendReceiverUser = receiverUser
						sendReceiverUserPhone = receiverUserPhone
						sendReceiverDeptId = depId
					}
					let deviceData = AddDeviceCard.$data.formData
					let departmentData = AddDeparmentCard.$data.formData
					let attachmentData = attachmentFormatter(AttachmentCard.$data.attachment)

					let factoryInfoData = []
					if (['1', '3'].includes(this.type)) {
						if (this.isChecked) {
							for (let i = 0; i < this.factoryList.length; i++) {
								let ref = this.$refs['AddFactoryCard' + i][0]
								let {
									factory,
									user,
									phone
								} = ref.$data.formData
								let obj = {
									manufacturerId: factory,
									manufacturerUser: user,
									manufacturerPhone: phone
								}
								factoryInfoData.push(obj)
							}
						} else {
							if (!AddFactoryInfoCard.validate()) {
								return
							}
							let AddFactoryInfoCardData = AddFactoryInfoCard.$data.formData
							let {
								factory,
								user,
								phone
							} = AddFactoryInfoCardData
							factoryInfoData.push({
								manufacturerId: factory,
								manufacturerUser: user,
								manufacturerPhone: phone
							})
						}
					}
					let params = {
						// 送修方式
						repairType: this.type,
						// 送修人\配送人
						repairUser: repairUser,
						// 送修电话\配送人电话
						repairPhone: repairPhone,
						// 接收人
						receiveUser: sendReceiverUser,
						// 接收人电话
						receivePhone: sendReceiverUserPhone,
						//  接收科室
						receiveDept: sendReceiverDeptId,
						// 设备编号
						deviceNum: deviceData.num,
						// 设备名称
						deviceName: deviceData.name,
						// 设备分类
						deviceType: deviceData.type,
						// 设备型号
						deviceModel: deviceData.model,
						// 设备序列号
						deviceSerialNum: deviceData.serialNum,
						// 设备数据
						deviceData: deviceData.data,
						// 设备备注信息
						deviceRemark: deviceData.memo,
						// 科室名称
						deptId: departmentData.depId,
						// 科室联系人
						deptUser: departmentData.user,
						// 科室联系电话
						phone: departmentData.phone,
						// 科室地址
						deptPlace: departmentData.addr,
						// 故障描述
						faultRemark: departmentData.remark,
						// 是否邀标（0.否，1.是）
						bidsFlag: this.isChecked ? 1 : 0,
						// 预约取件时间
						pickUpTime: !this.isChecked ? AddInviteCard?.$data.time || '' : '',
						// 报价截止时间
						quoteEndTime: this.isChecked ? AddInviteCard?.$data.time || '' : '',
						// 备注
						remark: departmentData.memo,
						// 附件
						enclosure: attachmentData,
						manufacturerInfos: JSON.stringify(factoryInfoData)
					}


					await ReapirApi.department.submitSendRepair(params)
					wx.showToast({
						title: '提交成功',
						icon: 'success',
						complete() {
							wx.redirectTo({
								url: SendRepairRouterConfig.Department.Index
							})
						}
					})
				}
			}

		}

	}
</script>

<style>
</style>
